#!/usr/bin/env python

import sys, json, StringIO, Image
from collections import defaultdict

def main (argv0, argv):
    COLOR_GRAY =  (128, 128, 128)
    datums = defaultdict(lambda: 0, {
        (255, 255, 255): 0,     # white
        (0, 0, 0): 1,           # black
        (255, 0, 0): 2,         # red
        (0, 0, 255): 3,         # blue
        (0, 255, 0): 4,         # green
        (255, 255, 0): 5,       # yellow
        (0, 255, 255): 6,       # cyan
        (255, 0, 255): 7,       # magenta
    })
    
    img_data = sys.stdin.read()
    ss = StringIO.StringIO(img_data)
    img = Image.open(ss)
    w, h = img.size
    i = 0
    block_data = []
    enemies = []
    for y in range(h):
        for x in range(w):
            color = img.getpixel((x, y))[:3]
            if color == COLOR_GRAY:
                enemies.append((x, y))
            datum = datums[color]
            block_data.append(datum)
    def flat_index (x, y):
        return y * w + x
    
    def march (target_datum, march_start, march_end, march_d):
        """march_end is exclusive"""
        march_dx, march_dy = march_d
        x, y = march_start
        low_x = None
        low_y = None
        while (x, y) != march_end:
            index = flat_index(x, y)
            datum = block_data[index]
            if datum == target_datum:
                low_x = low_x or x
                low_y = low_y or y
                # hi_n is inclusive
                hi_x = x
                hi_y = y
            x += march_dx
            y += march_dy
        if low_x is not None:
            player_x = (low_x + hi_x+1) / 2.0
            player_y = (low_y + hi_y+1) / 2.0
            return True, (player_x, player_y)
        else:
            return False, (None, None)
    
    stuffs = [
        (( 1, 0), ((  0,   1), (  0, h-1), (0,  1))),
        ((-1, 0), ((w-1,   1), (w-1, h-1), (0,  1))),
        
        (( 0, 1), ((  1,   0), (w-1,   0), (1,  0))),
        (( 0,-1), ((  1, h-1), (w-1, h-1), (1,  0))),
    ]
    player_p = None
    player_d = None
    final_d = None
    for stuff in stuffs:
        (dx, dy), args = stuff
        success, (player_x, player_y) = march(2, *args) # red; entrance gate
        if success:
            player_x += 2.0 * dx
            player_y += 2.0 * dy
            player_p = (player_x, player_y)
            player_d = (dx, dy)
        success, garbage = march(4, *args) # green; goal gate
        if success:
            final_d = (-dx, -dy)
    if player_p and player_d and final_d:
        dd = {
            "width": w,
            "height": h,
            "block_data": block_data,
            "player_x": player_p[0],
            "player_y": player_p[1],
            "player_dir_x": float(player_d[0]),
            "player_dir_y": float(player_d[1]),
            "final_dir_x": float(final_d[0]),
            "final_dir_y": float(final_d[1]),
            "enemies": [dict(x=e[0], y=e[1]) for e in enemies],
        }
        print json.dumps(dd)
    else:
        return "POOP POOP POOP THIS SHOULD NEVER HAPPEN"

if __name__ == "__main__":
    try:
        exit_code = main(sys.argv[0], sys.argv[1:])
    except KeyboardInterrupt:
        exit_code = 1
    sys.exit(exit_code)
